package com.chen.palmar.project.set;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.entity.SetShopEntity;
import com.chen.palmar.entity.UserInfoEntity;
import com.primb.androidlibs.net.entity.MessageEvent;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import rx.Subscription;

/**
 * @author Chen
 * @date 2018/1/10
 * 功能描述：
 */

public class ShopManagerActivity extends BaseActivity {
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.rv_shop_manager)
    RecyclerView rvShopManager;
    private BaseQuickAdapter<SetShopEntity.DataBean, BaseViewHolder> adapter;
    private int factoryId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册事件
        EventBus.getDefault().register(this);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> ShopManagerActivity.this.finish());
        titleBar.setText("开设店铺");

        UserInfoEntity.DataBean info = (UserInfoEntity.DataBean) getIntent().getSerializableExtra("info");
        factoryId = info.getMerchantId();

        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvShopManager.setLayoutManager(layoutManager);

        adapter = new BaseQuickAdapter<SetShopEntity.DataBean, BaseViewHolder>(R.layout.item_shop_manager) {
            @Override
            protected void convert(BaseViewHolder helper, SetShopEntity.DataBean item) {
                helper.setText(R.id.tv_item_shop_name, item.getCategoryName());
                ImageView head = (ImageView) helper.getView(R.id.iv_item_shop);
                if (item.getStores() != null) {
                    Picasso.with(ShopManagerActivity.this).load(item.getStores().getLogo()).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(head);
                } else {
                    Picasso.with(ShopManagerActivity.this).load(R.mipmap.store_add_icon).placeholder(R.mipmap.store_add_icon).error(R.mipmap.store_add_icon).into(head);
                }
            }
        };
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (((SetShopEntity.DataBean) adapter.getData().get(position)).getStores() != null) {
                    Intent intent = new Intent(ShopManagerActivity.this, EditShopActivity.class);
                    intent.putExtra("info", getIntent().getSerializableExtra("info"));
                    intent.putExtra("name", ((SetShopEntity.DataBean) adapter.getData().get(position)).getCategoryName());
                    intent.putExtra("entity", ((SetShopEntity.DataBean) adapter.getData().get(position)));
                    intent.putExtra("factoryId", factoryId + "");
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(ShopManagerActivity.this, ApplyShopActivity.class);
                    intent.putExtra("info", getIntent().getSerializableExtra("info"));
                    intent.putExtra("name", ((SetShopEntity.DataBean) adapter.getData().get(position)).getCategoryName());
                    intent.putExtra("entity", ((SetShopEntity.DataBean) adapter.getData().get(position)));
                    intent.putExtra("factoryId", factoryId + "");
                    startActivity(intent);
                }
            }
        });
        rvShopManager.setAdapter(adapter);
        getListData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_manager;
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        Subscription subscribe = DataCenter.userShopList(map).subscribe(new HttpSubscriber<SetShopEntity>(this, showLoading()) {

            @Override
            public void onNext(SetShopEntity entity) {
                ArrayList<SetShopEntity.DataBean> list = new ArrayList<>();
                for (int i = 0; i < 5 - entity.getData().size(); i++) {
                    list.add(null);
                }
                list.addAll(0, entity.getData());
                adapter.setNewData(list);
            }
        });
        subscription.add(subscribe);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent) {
        switch (messageEvent.getEventType()) {
            case ApplyShopActivity.EVENT_SHOP_CODE:
                getListData();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
